<?php
	function habilitation()
	{
		$hab = array(
			1,
			2
		);
		
		if (in_array(get_s('bo_user_role'), $hab))
			return true;
		return false;
	}
	
	function view($table, $id)
	{
		if (habilitation())
		{
			$retour = array();
			$php = 'if ($cle == trad("Vignette", APPLNG))
					{
						$url_image = URL_SITE . "img/products/" . $valeur . "_thumb.jpg";
						$url_image = str_replace("admin/", "", $url_image);
						$alt = (!is_utf8($this -> tableau[trad("Nom", APPLNG)])) ? utf8_encode($this -> tableau[trad("Nom", APPLNG)]) : $this -> tableau[trad("Nom", APPLNG)]; 
						$valeur = "<img alt=\'" . $alt . "\' title=\'" . $alt . "\' src=\'" . $url_image . "\' />";
					}';
			$retour["php"] = $php;
			$champs = champs();
			$champaff = '';
			foreach ($champs as $champ)
				$champaff .= $champ['id'] . ', ';
			$champaff = substr($champaff, 0, -2);
			$gp_db = GP_db::getInstance(BDD_DSN, BDD_UTILISATEUR, BDD_MOT_DE_PASSE);
			$sql = "SELECT $champaff FROM $table WHERE id = $id";
			$tab = $gp_db -> fetchArray_one($sql);
			
			foreach ($tab as $cle => $valeur)
			{
				$type = $champs[$cle]["type"];
				if ($type == 'input')
					$retour[$champs[$cle]["Affichage"]] = sl($valeur);
				if ($type == 'textarea')
					$retour[$champs[$cle]["Affichage"]] = nl2br(sl($valeur));
				else if ($type == 'password')
					$retour[$champs[$cle]["Affichage"]] = '***';
				else if ($type == 'select')
				{
					$type_select = $champs[$cle]["typeselect"];
					
					if ($type_select == 'vocabulaire')
					{
						$vocabulaire = $champs[$cle]["data"];
						$tabvoc = explode(';', $vocabulaire);
						
						foreach ($tabvoc as $seg)
						{
							list($seg_c, $seg_v) = explode('%%', $seg, 2);
							if ($seg_c == $valeur)
							{
								$retour[$champs[$cle]["Affichage"]] = sl($seg_v);
								break;
							}
						}
					}
					
					else if ($type_select == 'sql')
					{
						$requete = $champs[$cle]["data"];
						$tagseg = $gp_db -> fetchRow($requete);
						foreach ($tagseg as $ligne)
						{
							if ($ligne[0] == $valeur)
							{
								$retour[$champs[$cle]["Affichage"]] = sl($ligne[1]);
								break;
							}
						}
					}
				}
			}
			
			return $retour;
		}
		return false;
	}
	
	function champs()
	{
		$retour = array();
		
		$retour['id_line']['id'] = 'id_line';
		$retour['id_line']['Affichage'] = 'Ligne produit';
		$retour['id_line']['type'] = 'select';
		$retour['id_line']['typeselect'] = 'sql';
		$retour['id_line']['data'] = 'SELECT id, name FROM _fo_lines';
		$retour['id_line']['premierchoix'] = trad('Choisir', APPLNG) . '%%';
		$retour['id_line']['defaut'] = '';
		$retour['id_line']['js'] = 'return false;';
		$retour['id_line']['obligatoire'] = true;
		
		$retour['ordre']['id'] = 'ordre';
		$retour['ordre']['Affichage'] = 'Ordre';
		$retour['ordre']['max'] = 3;
		$retour['ordre']['type'] = 'input';
		$retour['ordre']['obligatoire'] = true;
		
		$retour['num_image']['id'] = 'num_image';
		$retour['num_image']['Affichage'] = 'Vignette';
		$retour['num_image']['max'] = 10;
		$retour['num_image']['type'] = 'input';
		$retour['num_image']['obligatoire'] = true;
		
		$retour['link_eshop']['id'] = 'link_eshop';
		$retour['link_eshop']['Affichage'] = 'Lien eshop';
		$retour['link_eshop']['max'] = 255;
		$retour['link_eshop']['type'] = 'input';
		$retour['link_eshop']['obligatoire'] = true;
		
		$retour['name']['id'] = 'name';
		$retour['name']['Affichage'] = 'Nom';
		$retour['name']['max'] = 255;
		$retour['name']['type'] = 'input';
		$retour['name']['obligatoire'] = true;
		
		$retour['type']['id'] = 'type';
		$retour['type']['Affichage'] = 'Type';
		$retour['type']['max'] = 255;
		$retour['type']['type'] = 'input';
		$retour['type']['obligatoire'] = true;
		
		$retour['contenance']['id'] = 'contenance';
		$retour['contenance']['Affichage'] = 'Contenance';
		$retour['contenance']['max'] = 255;
		$retour['contenance']['type'] = 'input';
		$retour['contenance']['obligatoire'] = true;
		
		$retour['description']['id'] = 'description';
		$retour['description']['Affichage'] = 'Description';
		$retour['description']['max'] = 5000;
		$retour['description']['type'] = 'textarea';
		$retour['description']['obligatoire'] = true;
		
		$retour['fragrances']['id'] = 'fragrances';
		$retour['fragrances']['Affichage'] = 'Fragrances';
		$retour['fragrances']['max'] = 5000;
		$retour['fragrances']['type'] = 'textarea';
		$retour['fragrances']['obligatoire'] = true;
		
		$retour['meta_description']['id'] = 'meta_description';
		$retour['meta_description']['Affichage'] = 'Meta description';
		$retour['meta_description']['max'] = 5000;
		$retour['meta_description']['type'] = 'textarea';
		$retour['meta_description']['obligatoire'] = true;
		
		$retour['meta_keywords']['id'] = 'meta_keywords';
		$retour['meta_keywords']['Affichage'] = 'Meta keywords';
		$retour['meta_keywords']['max'] = 5000;
		$retour['meta_keywords']['type'] = 'textarea';
		$retour['meta_keywords']['obligatoire'] = true;
		
		$retour['page_title']['id'] = 'page_title';
		$retour['page_title']['Affichage'] = 'Titre page';
		$retour['page_title']['max'] = 255;
		$retour['page_title']['type'] = 'input';
		$retour['page_title']['obligatoire'] = true;
		
		$retour['alt_index']['id'] = 'alt_index';
		$retour['alt_index']['Affichage'] = 'Alt index';
		$retour['alt_index']['max'] = 255;
		$retour['alt_index']['type'] = 'input';
		$retour['alt_index']['obligatoire'] = true;
		
		$retour['alt_page_product']['id'] = 'alt_page_product';
		$retour['alt_page_product']['Affichage'] = 'Alt page produit';
		$retour['alt_page_product']['max'] = 255;
		$retour['alt_page_product']['type'] = 'input';
		$retour['alt_page_product']['obligatoire'] = true;

		return $retour;
	}
	
	
	$this -> setvar('titre', 'Afficher un produit');
	$this -> setvar('retour_url', rw('contenus'));
	$this -> setvar('habilitation', habilitation());
	$this -> setvar('tableau', view($this -> table, $this -> id));
?>